﻿
Partial Class Administracion_Permisos
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Page.IsPostBack = False Then
            cargarPerfiles()
        End If
    End Sub

    Sub cargarPerfiles()
        Using bd As New bdSeguridadDataContext
            Dim a = From p In bd.Web_Perfiles
                    Select p

            ddrPerfiles.DataSource = a
            ddrPerfiles.DataTextField = "Perfil"
            ddrPerfiles.DataValueField = "CodPerfil"
            ddrPerfiles.DataBind()
        End Using
        ddrPerfiles.Items.Insert(0, "Seleccione un perfil")

    End Sub

    Sub CargarPermisos(ByVal CodPerfil As Integer)
        Using bd As New bdSeguridadDataContext
            Dim a = bd.Web_SP_GetPermisos(CodPerfil)
            grdPermisos.DataSource = a
            grdPermisos.DataBind()
        End Using
    End Sub

    Protected Sub ddrPerfiles_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddrPerfiles.SelectedIndexChanged
        CargarPermisos(ddrPerfiles.SelectedValue)
    End Sub

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        guardar()
    End Sub
    Sub guardar()
        Dim resultados As XElement = <PP>
                                         <%= From fila As GridViewRow In Me.grdPermisos.Rows _
                                             Select <P
                                                        SM=<%= fila.Cells(0).Text %>
                                                        I=<%= IIf(CType(fila.FindControl("chkIns"), CheckBox).Checked, 1, 0) %>
                                                        E=<%= IIf(CType(fila.FindControl("chkEd"), CheckBox).Checked, 2, 0) %>
                                                        D=<%= IIf(CType(fila.FindControl("chkEl"), CheckBox).Checked, 3, 0) %>
                                                        C=<%= IIf(CType(fila.FindControl("chkCon"), CheckBox).Checked, 4, 0) %>
                                                        >
                                                    </P> %>
                                     </PP>
        Dim idperfil As Integer = ddrPerfiles.SelectedValue

        Using bd As New bdSeguridadDataContext()
            bd.Web_SP_PerfilesXSubmodulosXPermisosSave(idperfil, resultados)
            bd.SubmitChanges()
            bd.registrarLog(Context.User.Identity.Name, 4, Request.CurrentExecutionFilePath)
        End Using
    End Sub
End Class
